from PyQt5.QtGui import QIcon, QFont, QPalette, QColor
from PyQt5.QtWidgets import (QWidget, QVBoxLayout, QPushButton, QLabel,
                             QLineEdit, QHBoxLayout, QMessageBox, QFrame)
from PyQt5.QtCore import Qt, QSize
import requests
import threading
from admin_menu import AdminMenu


class Admin_Login(QWidget):
    def __init__(self, parent=None):
        super().__init__()
        self.parent = parent
        self.clear_layout=self.parent.clear_layout
        self.create_scroll=self.parent.create_scroll
        # 窗口基础设置
        self.setWindowTitle('管理员登录页面')
        self.resize(500, 300)
        # 设置窗口图标
        try:
            icon = QIcon('../static/image/登录.png')
            self.setWindowIcon(icon)
        except:
            pass

        # 全局样式设置
        self._setup_styles()

        # 主布局
        mainLayout = QVBoxLayout(self)
        mainLayout.setContentsMargins(80, 150, 80, 150)  # 边距调整
        mainLayout.setSpacing(30)  # 控件间距
        self.initUI(mainLayout)

    def _setup_styles(self):
        """设置全局样式"""
        # 字体设置
        font = QFont("Microsoft YaHei", 12)
        self.setFont(font)

        # 窗口背景色
        palette = QPalette()
        palette.setColor(QPalette.Window, QColor(248, 249, 250))
        self.setPalette(palette)

        # 全局样式表
        self.setStyleSheet("""
            QLabel#titleLabel {
                color: #2C3E50;
                font-size: 24px;
                font-weight: bold;
            }
            QLabel {
                color: #34495E;
                font-size: 14px;
            }
            QLineEdit {
                height: 45px;
                border: 1px solid #DDDDDD;
                border-radius: 6px;
                padding-left: 15px;
                padding-right: 15px;
                background-color: white;
                font-size: 13px;
            }
            QLineEdit:focus {
                border-color: #3498DB;
                outline: none;
            }
            QPushButton {
                height: 45px;
                border-radius: 6px;
                font-size: 14px;
                font-weight: 500;
            }
        """)

    def initUI(self, mainLayout):
        # 标题
        title_label = QLabel('管理员登录')
        title_label.setObjectName("titleLabel")
        title_label.setAlignment(Qt.AlignCenter)
        mainLayout.addWidget(title_label)

        # 账号输入区域
        layout1 = QHBoxLayout()
        layout1.setSpacing(15)
        mainLayout.addLayout(layout1)

        label = QLabel('账号')
        label.setMinimumWidth(50)
        label.setAlignment(Qt.AlignRight | Qt.AlignVCenter)
        layout1.addWidget(label)

        self.num_input = QLineEdit()
        self.num_input.setPlaceholderText('请输入账号')
        self.num_input.setStyleSheet("""
            QLineEdit {
                border: 1px solid #DDD;
                border-radius: 6px;
                padding: 0 15px;
                background: white;
            }
            QLineEdit:focus {
                border-color: #3498DB;
                box-shadow: 0 0 0 2px rgba(52, 152, 219, 0.2);
            }
        """)
        layout1.addWidget(self.num_input)

        # 密码输入区域
        layout2 = QHBoxLayout()
        layout2.setSpacing(15)
        mainLayout.addLayout(layout2)

        label = QLabel('密码')
        label.setMinimumWidth(50)
        label.setAlignment(Qt.AlignRight | Qt.AlignVCenter)
        layout2.addWidget(label)

        self.pwd_input = QLineEdit()
        self.pwd_input.setPlaceholderText('请输入密码')
        self.pwd_input.setEchoMode(QLineEdit.Password)
        self.pwd_input.setStyleSheet("""
            QLineEdit {
                border: 1px solid #DDD;
                border-radius: 6px;
                padding: 0 15px;
                background: white;
            }
            QLineEdit:focus {
                border-color: #3498DB;
                box-shadow: 0 0 0 2px rgba(52, 152, 219, 0.2);
            }
        """)
        layout2.addWidget(self.pwd_input)

        # 按钮区域
        btn_layout = QHBoxLayout()
        btn_layout.setSpacing(15)
        mainLayout.addLayout(btn_layout)

        # 确认按钮
        sub_btn = QPushButton('确认')
        sub_btn.setStyleSheet("""
            QPushButton {
                background-color: #3498DB;
                color: white;
                border: none;
            }
            QPushButton:hover {
                background-color: #2980B9;
            }
            QPushButton:pressed {
                background-color: #2471A3;
            }
        """)
        btn_layout.addWidget(sub_btn)

        # 取消按钮
        exit_btn = QPushButton('取消')
        exit_btn.setStyleSheet("""
            QPushButton {
                background-color: #ECF0F1;
                color: #34495E;
                border: 1px solid #DDD;
            }
            QPushButton:hover {
                background-color: #DEE2E6;
            }
        """)
        btn_layout.addWidget(exit_btn)

        mainLayout.addStretch()

        sub_btn.clicked.connect(self.sub_click)
        exit_btn.clicked.connect(self.exit)

    def sub_click(self):
        num=self.num_input.text()
        pwd=self.pwd_input.text()
        par={
            'username': num,
            'password':pwd
        }
        response=requests.post('http://127.0.0.1:8000/admin/login',json=par)
        dic=response.json()
        if dic.get('code') != 200:
            QMessageBox.critical(None,'错误','密码或账号错误请重试')
        else:
            self.window=AdminMenu(self)
            self.window.show()
            self.close()

    def exit(self):
        if self.parent:
            self.parent.show()
        self.close()  # 保留原有逻辑
